package com.thunder.soft.vm;

import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.thunder.soft.bean.UserBean;
import com.thunder.soft.common.base.BaseBean;
import com.thunder.soft.common.base.BaseViewModel;
import com.thunder.soft.common.net.HttpClient;
import com.thunder.soft.ui.activity.RegisterActivity;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.RequestBody;
import okhttp3.Response;

public class LoginViewModel extends BaseViewModel {
    private static String TAG = LoginViewModel.class.getName();
    public MutableLiveData<UserBean> liveData = new MutableLiveData<>();
    public ObservableField<String> userName = new ObservableField<>();
    public ObservableField<String> passWord = new ObservableField<>();

    @Override
    public void onCreate(@NonNull LifecycleOwner owner) {
        super.onCreate(owner);
    }

    public void onLoginClick() {
        if (TextUtils.isEmpty(userName.get())) {
            getErrorMsg().postValue("请输入账号");
            return;
        }

        if (TextUtils.isEmpty(passWord.get())) {
            getErrorMsg().postValue("请输入密码");
            return;
        }

        RequestBody requestBody = new FormBody.Builder()
                .add("username", Objects.requireNonNull(userName.get()))
                .add("password", Objects.requireNonNull(passWord.get()))
                .build();

        HttpClient.getInstance().request("user/login", "POST", requestBody, new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                getErrorMsg().postValue(e.getMessage());
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                Headers headers = response.headers();
                List<String> cookie = headers.values("Set-Cookie");
                HttpClient.getInstance().setCookie(cookie);
                Gson gson = new Gson();
                String responseStr = response.body().string();
                BaseBean<UserBean> userBean = gson.fromJson(responseStr,
                        new TypeToken<BaseBean<UserBean>>() {
                        }.getType());

                if (userBean.getErrorCode() == 0) {
                    HttpClient.getInstance().setPassword(passWord.get());
                    HttpClient.getInstance().setUserName(userName.get());
                    liveData.postValue(userBean.getData());
                } else {
                    getErrorMsg().postValue(userBean.getErrorMsg());
                }
            }
        });
    }

    public void onBackPressed() {
        getFinishActivity().postValue(1);
    }

    public void toRegisterActivity() {
        HashMap<String, Object> value = new HashMap<>();
        value.put("activity", RegisterActivity.class);
        getActivityLiveData().postValue(value);
    }

    public MutableLiveData<UserBean> getLiveData() {
        return liveData;
    }
}
